//
//  MainContentCell.m
//  nuoee_krypto
//
//  Created by Mac on 2018/5/31.
//  Copyright © 2018年 nuoee. All rights reserved.
//

#import "MainContentCell.h"
#import "LeftCoinRankView.h"
#import "ContentCollectionViewCell.h"
#import "LeftExchangeView.h"

static NSString *contentCellId = @"contentCellId";
static NSString *contentCollectionCellId = @"contentCollectionCellId";

@interface MainContentCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) LeftCoinRankView *rankView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, assign) CoinRankOrExchangeType type;
@property (nonatomic, strong) LeftExchangeView *exchangeView;


@end

@implementation MainContentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier type:(CoinRankOrExchangeType)type{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.dataArray = @[@"￥46,322.70",@"+300.37%",@"313.04亿",@"￥7,949.82亿",@"1,706万",@"81.24%",@"2,100万"];
        
        self.type = type;
        self.separatorLine = NO;
        [self createUI];
    }
    return self;
}

- (instancetype)initMainContentCell:(UITableView *)tableView type:(CoinRankOrExchangeType)type{
    self = [tableView dequeueReusableCellWithIdentifier:contentCellId];
    if (!self) {
        self = [[MainContentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contentCellId type:type];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return self;
}

+ (instancetype)mainContentCell:(UITableView *)tableView type:(CoinRankOrExchangeType)type{
    return [[MainContentCell alloc] initMainContentCell:tableView type:type];
}

- (void)setIndex:(NSInteger)index{
    _index = index;
    if (index % 2 != 0) {
        self.contentView.backgroundColor = MainDarkColor;
    }else{
        self.contentView.backgroundColor = MainBlackColor;
    }
}

#pragma mark - collection datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
    ContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:contentCollectionCellId forIndexPath:indexPath];
    cell.type = self.type;
    cell.index = indexPath.item;
    cell.content = self.dataArray[indexPath.item];
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
    if ([scrollView isKindOfClass:[UICollectionView class]]) {
        if (scrollView.contentOffset.y != 0) {
            scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 0);
            return;
        }
        if (self.offsetBlock) {
            self.offsetBlock(scrollView.contentOffset);
        }
    }
}

#pragma mark - UI
- (void)createUI{
    
    if (self.type == CoinRankType) {
        [self.contentView addSubview:self.rankView];
        [self.rankView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.mas_equalTo(self.contentView);
            make.width.mas_equalTo(self.contentView.mas_width).multipliedBy(0.45);
        }];
        
        [self.contentView addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.mas_equalTo(self.contentView);
            make.left.mas_equalTo(self.rankView.mas_right);
        }];
    }else{
        [self.contentView addSubview:self.exchangeView];
        [self.exchangeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.mas_equalTo(self.contentView);
            make.width.mas_equalTo(self.contentView.mas_width).multipliedBy(0.4);
        }];
        
        [self.contentView addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.mas_equalTo(self.contentView);
            make.left.mas_equalTo(self.exchangeView.mas_right);
        }];
    }
}

#pragma mark - init
- (LeftCoinRankView *)rankView{
    if (!_rankView) {
        _rankView = [[LeftCoinRankView alloc] init];
    }
    return _rankView;
}

- (LeftExchangeView *)exchangeView{
    if (!_exchangeView) {
        _exchangeView = [[LeftExchangeView alloc] init];
    }
    return _exchangeView;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.itemSize = CGSizeMake((MAINSCREEN_WIDTH  * (self.type == CoinRankType ? 0.55 : 0.6)) / 2, AdaptY(47));
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[ContentCollectionViewCell class] forCellWithReuseIdentifier:contentCollectionCellId];
    }
    return _collectionView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
